package sys.cloud.escape;

/**
 * Created by shiyusen on 2017/3/23.
 */
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class MyRequestWrapper extends HttpServletRequestWrapper {
    public MyRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    @Override
    public String getParameter(String name) {
        String value = super.getParameter(name);
        if(value!=null){
            return toHtml(value.trim());
        }else{
            return null;
        }
    }

    //转化特殊字符
    public String toHtml(String str){
        if(str==null){
            return null;
        }
        StringBuffer sb = new StringBuffer();
        int len = str.length();
        for(int i=0;i<len;i++){
            char c= str.charAt(i);
            switch(c){
                case ' ':sb.append(" ");break;
                case '\n':sb.append("<br/>");break;
                case '\r':break;
                case '\'':sb.append("'");break;
                case '<':sb.append("<");break;
                case '>':sb.append(">");break;
                case '&':sb.append("&");break;
                case '"':sb.append("\"");break;
                case '\\':sb.append("\\");break;
                default: sb.append(c);break;
            }
        }
        return sb.toString();
    }
}